package com.wei;

/**
 * @Author :sunwei
 * @Description:
 * @Date create in 22:44  2021/5/10
 * @Modified by:
 */
public class DeadLockTest {
    public StringBuilder builder1 = new StringBuilder();
    public StringBuilder builder2 = new StringBuilder();
    public void test(){
        Thread thread1 = new Thread(()->{
            synchronized (builder1){
                builder1.append("a");
                builder1.append("b");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (builder2){
                    builder2.append("1");
                    builder2.append("2");
                }
            }
        });
        Thread thread2 = new Thread(()->{
            synchronized (builder2){
                builder2.append("a");
                builder2.append("b");
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (builder1){
                    builder1.append("1");
                    builder1.append("2");
                }
            }
        });
        thread1.start();
        thread2.start();
    }
    public static void main(String[] args) {
        DeadLockTest test = new DeadLockTest();
        test.test();
    }
}
